package threadsafe;

/**
 * Created by 5406160@qq.com on LiBoWen 李博闻.
 *
 * @ClassName : SellTicket1
 * @Author : 54061
 * @Date: 2022/2/15 18:08
 */
public class SellTicket1 implements Runnable{
    //总票数 : 共享数据
    int ticket = 100;
    //定于锁对象
    Object obj = new Object();

    @Override
    public void run() {
        //火车站不关门
        while (true){
            synchronized (obj){
                //判断
                if (ticket > 0){
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();

                    }
                    //买票
                    System.out.println(Thread.currentThread().getName()+"买了第"+ticket+"张票");
                    //真正的买票
                    ticket--;


                }
            }
        }
    }
}
